using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
using System.ServiceModel.Activation;

using System.Web.Mvc;
using ZDSoft.LMS.Core;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord;
using ZDSoft.LMS.Domain;
using ZDSoft.LMS.Manager;

namespace ZDSoft.LMS.Web
{
    public class Global : System.Web.HttpApplication
    {

        #region Fields

        private Container container;

        #endregion

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "User", action = "Login", id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected void Application_Start(object sender, EventArgs e)
        {

            IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
            ActiveRecordStarter.Initialize(typeof(ZDSoft.LMS.Domain.User).Assembly, source);

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            container = Container.Instance;
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Server.Transfer("~/Error.aspx");
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            container.Dispose();
        }
    }
}